package com.example.vaccine.controller;

import com.example.vaccine.VO.PageVO;
import com.example.vaccine.VO.ResultVO;
import com.example.vaccine.entity.VaccineFeedback;
import com.example.vaccine.service.VaccineFeedbackService;
import com.example.vaccine.util.ResultVOUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/vaccineFeedback")
public class VaccineFeedbackController {

    @Autowired
    private VaccineFeedbackService feedbackService;

    @Operation(summary = "分页获取反馈列表", description = "根据时间范围分页查询疫苗反馈")
    @GetMapping
    public ResultVO getFeedbacks(
            @Parameter(description = "时间范围，如7days,30days", required = true) @RequestParam String timeRange,
            @Parameter(description = "页码，从1开始", required = true) @RequestParam Integer pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam Integer pageSize) {
        PageVO pageVO = feedbackService.getFeedbacksByTimeRange(timeRange, pageNum, pageSize);
        return ResultVOUtil.success(pageVO);
    }

    @Operation(summary = "删除反馈", description = "根据反馈ID删除反馈记录")
    @DeleteMapping("/{id}")
    public ResultVO deleteFeedback(
            @Parameter(description = "反馈ID", required = true) @PathVariable Integer id) {
        boolean removed = feedbackService.removeById(id);
        return removed ? ResultVOUtil.success(null) : ResultVOUtil.fail();
    }

    @Operation(summary = "导出反馈Excel", description = "导出符合条件的疫苗反馈数据为Excel文件")
    @GetMapping("/export")
    public void exportFeedbacks(
            HttpServletResponse response,
            @Parameter(description = "时间范围，可选") @RequestParam(required = false) String timeRange,
            @Parameter(description = "搜索关键词，可选") @RequestParam(required = false) String search) throws Exception {
        List<VaccineFeedback> list = feedbackService.listFeedbacksForExport(timeRange, search);

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("疫苗反馈");

        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("疫苗编号");
        header.createCell(1).setCellValue("医生编号");
        header.createCell(2).setCellValue("反馈时间");
        header.createCell(3).setCellValue("反馈内容");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy--MM--dd");
        int rowIdx = 1;
        for (VaccineFeedback fb : list) {
            Row row = sheet.createRow(rowIdx++);
            row.createCell(0).setCellValue(fb.getVaccineNumbering());
            row.createCell(1).setCellValue(fb.getDoctorId());
            String dateStr = "";
            if (fb.getFeedbackTime() != null) {
                dateStr = sdf.format(fb.getFeedbackTime());
            }
            row.createCell(2).setCellValue(dateStr);
            row.createCell(3).setCellValue(fb.getFeedbackText());
        }

        String fileName = URLEncoder.encode("疫苗反馈.xlsx", "UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

        OutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
        workbook.close();
    }

    @Operation(summary = "根据医生ID获取反馈列表", description = "查询指定医生的所有疫苗反馈记录")
    @GetMapping("/list")
    public ResultVO<List<VaccineFeedback>> listByDoctorId(
            @Parameter(description = "医生ID", required = true) @RequestParam Integer doctorId) {
        List<VaccineFeedback> feedbackList = feedbackService.listByDoctorId(doctorId);
        return ResultVOUtil.success(feedbackList);
    }
}
